// 选择题

const {
    getById,    // 根据id获取题数据
    getList,    // 获取题列表
    create,     // 添加题
    edit,       // 编辑题
    remove,     // 删除题
} = require("../../service/question/choice")

exports.choiceApi = async ctx => {
    // 获取url 字段 继而判断接下来操作
    let url = ctx.request.url.split("/")[1];
    const { id } = ctx.params || "";
    const { title, pageNum, pageSize, chapter_id, options, answer, creator, updator, usage, difficulty } = ctx.request.body || "";
    let res;
    switch (ctx.request.method) {
        case "GET": if (ctx.request.url.length > url.length + 1) {
            res = await getById({ id })
        } else {
            res = await getList({ pageNum, pageSize, chapter_id })
        }; break;
        case "POST": res = await create({ title, chapter_id, options, answer, creator, creator, usage, difficulty }); break;
        case "DELETE": res = await remove({ id }); break;
        case "PUT": res = await edit({ id, title, chapter_id, options, answer, creator, updator, usage, difficulty }); break;
        default: res = { msg: "操作失败 请求方法不规范", code: "400" }
    }
    ctx.body = res
}